//
//  FFWorld.h
//  ForceField
//
//  Created by Roxanne Brittain on 11/23/11.
//  Copyright (c) 2011 Digifit. All rights reserved.
//

#import <Foundation/Foundation.h>
#include <vector>
#import "FFObject.h"
#import "FFObjectWrapper.h"
#import "FFVector.h"
#import "FFFieldHandler.h"
#import "FFCollisionHandler.h"

#ifndef ForceField_FFWorld_h
#define ForceField_FFWorld_h

class FFWorld { 
    
public:
    CGRect worldBounds;
    FFObjectWrapper* addNewObject();
    void refreshWithSeconds(float s);
    void setDeviceOrientation(UIDeviceOrientation o) {orientation=o;}
    
    FFWorld() : gravityAcceleration(new FFVector(0, 98.1)), nextIDNumber(100), playing(1) {
        collisionHandler = new FFCollisionHandler();
    }
    FFWorld(FFVector *gravity) : gravityAcceleration(gravity), nextIDNumber(100) {}
    
    //rotate the gravity vector based on device orientation
    FFVector* getGravity() {
        FFVector *gravity;
        switch (orientation) {
            case UIDeviceOrientationPortrait:
                gravity = gravityAcceleration;
                break;
            case UIDeviceOrientationLandscapeLeft:
                gravity = new FFVector(-1*gravityAcceleration->getY(), gravityAcceleration->getX());
                break;
            case UIDeviceOrientationLandscapeRight:
                gravity = new FFVector(gravityAcceleration->getY(), gravityAcceleration->getX());
                break;
            case UIDeviceOrientationPortraitUpsideDown:
                gravity = new FFVector(gravityAcceleration->getX(), -1*gravityAcceleration->getY());
                break;
            case UIDeviceOrientationUnknown:
            case UIDeviceOrientationFaceUp:
            case UIDeviceOrientationFaceDown:
                gravity = new FFVector(0,0);
                break;
            default:
                NSLog(@"orient: %d", orientation);
                break;
        }
        return gravity;
    }
    
    bool playing;
    int nextIDNumber;
    
    void clearObjects() {m_objectVector.clear();}
    void removeObject(FFObject *o) 
    {
        std::vector<FFObject*>::iterator it;
        
        for (int i = 0; i<m_objectVector.size(); i++) {
            FFObject *curObject = m_objectVector.at(1);
            if (curObject->idNumber == o->idNumber) {
                m_objectVector.erase(m_objectVector.begin()+i);
                break;
            }
        }
    }
    
    BOOL objectOverlaps(FFObject *o)
    {
        return collisionHandler->objectOverlaps(m_objectVector, o);
    }
    
    int objectCount() {return m_objectVector.size();}
    
private:
    
    UIDeviceOrientation orientation;
    std::vector<FFObject*> m_objectVector;
    FFFieldHandler *fieldHandler;
    FFCollisionHandler *collisionHandler;
    
    FFVector *gravityAcceleration;
    
};

#endif